package cate.game.role.res.guild.po.donate;

import cate.common.table.d.GDFunc;
import cate.common.table.d.GDGuild;
import cate.common.table.d.GDObj;
import cate.game.res.MixRes;
import cate.game.res.MixResItem;
import cate.common.table.guild.GuildDonateRow;
import cate.game.role.res.guild.msg.donate.MyGuildDonateResp;
import cate.game.role.res.guild.po.GuildLogDetailPO;
import cate.game.role.res.guild.po.GuildPO;
import cate.game.role.res.guild.po.my.MyGuildFuncBase;
import easy.java.dev.note.NoteField;

import java.util.HashMap;
import java.util.Map;

import cate.game.role.Role;
import easy.java.practice.errorcode.EcResult;

public class MyGuildDonatePO extends MyGuildFuncBase {
    @NoteField(value = "上次捐献类型")
    public int lastDonateType;

    @NoteField(value = "上次捐献时间")
    public long lastDonateTime;

    @NoteField(value = "捐献的情况统计")
    public Map<Integer, Boolean> donateStatusMap;

    @NoteField(value = "上次捐献公会Id")
    public String lastDonateId;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (donateStatusMap == null) {
            donateStatusMap = new HashMap<>();
            resetDonateStatusMap();
        }
    }

    private void resetDonateStatusMap() {
        for (GuildDonateRow row : role.getGame().table.guild.donate.getList()) {
            donateStatusMap.put(row.id, false);
        }
    }

    @Override
    public void onEnter() {
        super.onEnter();
        if (lastDonateId == null) {
            if (role.getRes().guild.checkGuild().ok()) {
                lastDonateId = role.getRes().guild.uid;
            } else {
                lastDonateId = "";
            }
        }
    }

    /**
     * 跨天更新
     */
    public void onDaySpan() {
        resetDonateStatusMap();
        noticeUpdate();
    }

    /**
     * 公会捐献
     */
    public void donate(int id) {
        if (!guildFuncOpenCheck()) {
            return;
        }
        GuildPO guild = role.getRes().guild.getGuild();
        if (this.donateStatusMap.containsValue(true)){
            role.getGame().notice.message(role, 310409, "今日捐献次数已达上限");
            return;
        }

        GuildDonateRow row = role.getGame().table.guild.donate.get(id);
        if (row == null) {
            role.getGame().notice.message(role, 310410, "配置错误");
            return;
        }

        MixRes consume = new MixRes(row.consumeStr);
        EcResult<?> r = consume.consume(role, null);
        if (!r.ok()) {
            role.getGame().notice.message(role, "费用不足");
            return;
        }

        this.donateStatusMap.put(id, true);
        MixRes reward = new MixRes();
        //添加贡献货币奖励
        reward.addItem(new MixResItem(GDObj.Type.NUM, GDObj.Num.P, row.PNum));
        // 记录玩家本次捐献的类型和时间，并通知玩家捐献数据的更新
        reward.add(role);
        // 发放奖励通知
        role.getGame().notice.dialogReward(role, reward);
        role.getRes().guild.performance.addPValue(row.PNum);
        this.lastDonateType = id;
        refreshDonateTime(guild.uid);
        // 在公会基础中增加公会经验
        guild.basic.addExp(row.exp);
        guild.donate.log.addLog(new GuildLogDetailPO(GDGuild.log.DONATE_GUILD, role.getBase().name, row.exp));
        // 记录公会活跃任务
        role.getRes().guild.active.addActive(row.taskId);
        role.getRes().guild.active.addActive(GDGuild.activeTask.DONATE_COMMON);
        role.getHistory().action.guildDonate();
    }

    public void refreshDonateTime(String uid) {
        this.lastDonateTime = System.currentTimeMillis();
        this.lastDonateId = uid;
        this.noticeUpdate();
        // 在我的公会表现里面也同步这个时间,并记录公会贡献
        role.getRes().guild.performance.lastDonateTime = this.lastDonateTime;
    }

    public void noticeUpdate() {
        role.sendNow(new MyGuildDonateResp(this));
    }

    @Override
    public int getFuncId() {
        return GDFunc.GuildFunc.DONATE;
    }
}
